package com.matt.service.cookbook.impl;

import com.matt.domain.cookbook.RecipeFood;
import com.matt.repository.cookbook.RecipeFoodRepository;
import com.matt.repository.cookbook.RecipeNativeRepository;
import com.matt.service.cookbook.RecipeFoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class RecipeFoodServiceImpl implements RecipeFoodService {

	@Autowired
	RecipeNativeRepository nativeRepository;

	@Autowired
	RecipeFoodRepository foodRepository;

	@Override
	public List<RecipeFood> selectRecipeFoodListByCondition(Map<String, Object> filter) {
		List<RecipeFood> list = nativeRepository.selectRecipeFoodListByCondition(filter);
		return list;
	}

	@Override
	public RecipeFood selectRecipeFoodByName(String name, String areaId) {
		RecipeFood result = foodRepository.findFirstByNameAndAreaId(name, areaId);
		return result;
	}

	@Override
	public int insertRecipeFoodByTemplate(String areaId) {
		return foodRepository.insertRecipeFoodByTemplate(areaId);
	}

	@Override
	public RecipeFood createRecipeFood(String foodName, String areaId, Integer foodTypeId, String imagePath ) {
		RecipeFood food = new RecipeFood();

		food.setName(foodName);
		food.setAreaId(areaId);
		food.setType(foodTypeId);
		food.setTypeName("主食");
		food.setPhoto(imagePath);

		foodRepository.save(food);

		return food;
	}

	@Override
	public RecipeFood updateRecipeFood(RecipeFood food) {
		return foodRepository.save(food);
	}

	@Override
	public RecipeFood selectByAreaIdAndId(String areaId, Long id) {
		return foodRepository.findFirstByAreaIdAndId(areaId, id);
	}

	@Override
	public int deleteRecipeFood(RecipeFood food) {
		foodRepository.delete(food);
		return 1;
	}

	@Override
	public RecipeFood selectRecipeFoodByAreaIdAndName(String areaId, String foodName) {
		return foodRepository.findByAreaIdAndName(areaId,foodName);
	}

	@Override
	public RecipeFood insertRecipeFood(RecipeFood food) {
		return foodRepository.save(food);
	}

}
